# -*- codeing = utf-8 -*-
# @Time : 2021/2/28 17:09
# @Atuthor: 朱朱
# @File: file_manager.py
# @Software: PyCharm
import json

base_dir = './files/'

def read_text(file_name):
    try:
        with open(base_dir+ file_name, 'r', encoding='utf8') as file:
            msg = file.read()
            return msg
    except FileNotFoundError:
        print('读取文件失败,请检查路径')

def read_json(json_name, non_data):
        try:
            with open(base_dir + json_name, 'r', encoding='utf8') as file:
                msg = file.read()
                return json.loads(msg) # 将 json 转为 python
        except FileNotFoundError:
            return non_data # 定义的返回一个默认的{}字典

def write_json(json_name, data):
        with open(base_dir+json_name, 'w', encoding='utf8') as file:
            json.dump(data,file) # 将python 转为 json,并保存文件